package com.swm.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

public class FileManager {
	static FileManager 	fileMgr  = null;
	
	public static FileManager getInstance()
	{
		if ( fileMgr == null ){
			fileMgr = new FileManager();	
		}
		return fileMgr;
	}
	
	public String readFile( String filePath, String fileName )
	{	
		StringBuffer contents = new StringBuffer();		
		File inputFile = new File( filePath + fileName );
		if ( !inputFile.exists() )
		{
			System.out.println( "[FILE READ ERROR] FILE NOT EXIST");
			return "";
		}
			
		FileInputStream fis = null;
		try {
			fis = new FileInputStream( inputFile );			
			int c;
			while ((c = fis.read()) != -1)
				contents.append((char) c);					
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println( e.getMessage() );
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println( e.getMessage() );
		} finally {
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
		
		return contents.toString();
		
		/*
		StringBuffer sb = new StringBuffer();		
		BufferedReader rd;
		try {
			rd = new BufferedReader( new FileReader(new File(filePath + fileName)));
			String line;
			while ((line = rd.readLine()) != null) {
				sb.append(line);
			}			  
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}				  
		return sb.toString();
		*/
	}
	public String normalizeContents( String content )
	{
		StringBuffer sb = new StringBuffer();
		for ( int i = 0 ; i < content.length() ; i++  ){
			char ch = content.charAt(i);
			if ( ch != ' ' && ch  != '\n' && ch != '\t' && ch != '\r' ){
				sb.append( ch );
			}
		}
		return sb.toString();
	}
	
	public void writeFile( String filePath, String fileName, String contents )
	{		
		FileWriter writer = null;
		BufferedWriter bufferWriter = null;
		try {
			writer = new FileWriter(filePath + fileName);
			bufferWriter = new BufferedWriter(writer);

			bufferWriter.write(contents);
			bufferWriter.flush();		
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		} finally {
			
			try {
				bufferWriter.close();
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		
	}
	
	public void copyFile(String srcFilePath, String srcFileName, String destFilePath, String destFileName) {
		if ( destFileName.equals( "" ) )
			destFileName = srcFileName;				
		
		String contents = readFile( srcFilePath, srcFileName );
		writeFile( destFilePath, destFileName, contents );		
		System.out.println( "[COPY] " + srcFilePath + srcFileName + " -->> " + destFilePath + destFileName );
	}
	
	public void copyDirectory(String srcFilePath, String destFilePath ) {
		
		File srcDir = new File( srcFilePath );
		if ( srcDir.exists() && srcDir.isDirectory() )
		{
			File destDir = new File( destFilePath );
			if ( !destDir.exists() )
			{
				if ( !destDir.mkdirs() )					
					return;
				System.out.println( "[COPY][MKDIR] MAKE DESTINATION DIR = " + destFilePath  );
			}			
			
			File[] fileList = srcDir.listFiles();
			for( File file : fileList )
			{	
				if ( file.getName().contains( ".svn") )		
					continue;
				
				String contents = readFile( srcFilePath, file.getName() );
				writeFile( destFilePath, file.getName(), contents );		
				System.out.println( "[COPY] " + srcFilePath + file.getName() + " -->> " + destFilePath + file.getName() );
			}
		}
		else
		{
			System.out.println( "[COPY][ERROR] SRC Directory is not Exist : " + srcFilePath  );
		}
	}
	
	public void replaceContents(String filePath, String fileName, String findStr, String replaceStr ) {
		String contents = readFile( filePath, fileName );		
		if ( contents.contains( findStr ))
		{			
			contents = contents.replaceAll( findStr, replaceStr );						
			FileManager.getInstance().writeFile( filePath, fileName, contents );
			System.out.println( "[REPLACE CONTENT]["+ filePath+fileName +"] [" + findStr + "] >>> [" + replaceStr + "] " );
		}	
	}
	
	public void insertBeforeContents( String filePath, String fileName, String beforeStr, String addStr ){
		String contents = readFile( filePath, fileName );		
		if ( !contents.contains( addStr ))
		{
			int insertPos;
			if ( ( insertPos = contents.indexOf( beforeStr )) >= 0 )
			{
				StringBuffer sb = new StringBuffer( contents.substring(0, insertPos-1) );
				sb.append( addStr );
				sb.append( contents.substring( insertPos, contents.length()));
				FileManager.getInstance().writeFile( filePath, fileName, sb.toString() );
				System.out.println( "[INSERT-BEFORE-CONTENT]["+ filePath+fileName +"] = " + addStr );
			}
		}
	}
	
	public void insertAfterContents( String filePath, String fileName, String afterStr, String addStr ){
		String contents = readFile( filePath, fileName );		
		if ( !contents.contains( addStr ))
		{
			int insertPos;
			if ( ( insertPos = contents.indexOf( afterStr )) >= 0 )
			{				
				StringBuffer sb = new StringBuffer( contents.substring(0, insertPos + afterStr.length() ) );
				sb.append( addStr );
				sb.append( contents.substring( insertPos + afterStr.length(), contents.length()));
				FileManager.getInstance().writeFile( filePath, fileName, sb.toString() );
				System.out.println( "[INSERT-AFTER-CONTENT]["+ filePath+fileName +"] = " + addStr );
			}
		}
	}
	
}
